package com.wisemoresystem.seal.action_app;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wisemoresystem.seal.base.action.ProjBaseAction;
import com.wisemoresystem.seal.model.t_bank;
import com.wisemoresystem.seal.model.t_user;
import com.wisemoresystem.seal.service.MacWktimeService;
import com.wisemoresystem.seal.service.UserService;
import com.wisemoresystem.seal.util.abchinaFinger.FingerTools;
import com.wisemoresystem.seal.util.json.JsonTools;
import com.wisemoresystem.seal.web.SessionVal;
import com.wisemoresystem.seal.web.WebTools;

/**
 * .net调用获取员工,机构信息json
 */

@Controller("ixNetAction")
@Scope("prototype")
public class NetAction extends ProjBaseAction {

	@Autowired
	private UserService userService;
	@Autowired
	private MacWktimeService timeService;

	@RequestMapping("/login_net")
	@ResponseBody
	public String login_net(String uname, String pwd, String login_type,
			String product_info) {
		String info = null;
		if ("密码".equals(login_type)) {

			t_user user = userService.selectUserByUnameAndPwd(uname, pwd);
			if (user != null) {
				req.getSession().setAttribute(SessionVal.login_user, user);
				
//				logT.logM("登录", t_user.class, user.get_id());
				Map<String, Object> map = JsonTools.objToMap(user);
				t_bank bank = oc.findObjById(user.getBank_id(), t_bank.class);
				// if(!timeService.macTime(user.getBank_id())){
				// map.put("result", "out");
				// }else{
				logT.logM("登录", t_user.class, user.get_id());
				
				map.put("result", "true");
				map.put("bank_name", bank.getName());
				// }
				info = JsonTools.toJson(map);
			} else {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("result", "false");
				info = JsonTools.toJson(map);
			}
		} else if ("指纹".equals(login_type)) {

			t_user user = userService.selectUserByBoing(uname);
			Map<String, Object> map = JsonTools.objToMap(user);
			if (user != null) {
				// if(!timeService.macTime(user.getBank_id())){
				// map.put("result", "out");
				// }else{
				try {
					t_bank bank = userService.findObjById(user.getBank_id(),
							t_bank.class);

					boolean result = false;
					if (bank != null) {
						result = FingerTools.login(uname, pwd, product_info,
								bank.getCode());
					}
					if (result) {
						req.getSession().setAttribute(SessionVal.login_user,
								user);
						map.put("result", "true");
					} else {
						map.put("result", "false");
					}
				} catch (Exception ex) {
					ex.printStackTrace();
					map.put("result", "false");
				}
				// }
			}
			info = JsonTools.toJson(map);
		}
		return info;
	}

	@RequestMapping("/logout_net")
	@ResponseBody
	public String logout_net() {
		t_user user = WebTools.getCurrentUser();
		if (user != null) {
			logT.logM("退出", t_user.class, user.get_id());
		}

		req.getSession().invalidate();
		return "true";
	}

	@RequestMapping("/work_time")
	@ResponseBody
	public String work_time() {

		t_user user = oc.findObjById(WebTools.getCurrentUserId(), t_user.class);
		List<Map<String, Object>> list = timeService.wk_time(user.getBank_id());
		String json = JsonTools.toJson(list);

		return json;
	}

}
